package com.mapapp.flutter_mapapp.persenter;

import android.content.Context;
import android.util.Log;

import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.bean.LoginBean;
import com.mapapp.flutter_mapapp.bean.MapResult;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.http.ActivityLifeCycleEvent;
import com.mapapp.flutter_mapapp.http.Api;
import com.mapapp.flutter_mapapp.http.HttpUtil;
import com.mapapp.flutter_mapapp.http.ProgressSubscriber;
import com.mapapp.flutter_mapapp.util.SharePreferenceUtil;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.MapViewInterface;

import java.util.ArrayList;
import java.util.List;

import rx.subjects.PublishSubject;

public class MapPersenter {
    private static final String TAG = "MapPersenter";
    private MapViewInterface mView;
    private Context mContext;
    private PublishSubject<ActivityLifeCycleEvent> lifecycleSubject;

    public MapPersenter(MapViewInterface mView, Context mContext, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        this.mView = mView;
        this.mContext = mContext;
        this.lifecycleSubject = lifecycleSubject;

        init();
    }

    private void init(){
        getFirstMapInfo();
    }

    public void getFirstMapInfo(){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getfirstdetails(),
                new ProgressSubscriber<MapResult>(mContext,false) {
                    @Override
                    protected void _onNext(MapResult data) {
                        if(data.getDetails().size() > 0){
                            mView.addMarks(data);
                        }else{
                            mView.noMapData();
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void getMapInfoByLevel(int level){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getdetailsbylevel(level),
                new ProgressSubscriber<MapResult>(mContext,false) {
                    @Override
                    protected void _onNext(MapResult data) {
                        if(data.getDetails().size() > 0){
                            mView.addMarks(data);
                        }else{
                            ToastUtils.showMessage(mContext, "暂无数据");
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void getdetailsbylevelandbh(String bh,int level){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getdetailsbylevelandbh(bh,level),
                new ProgressSubscriber<MapResult>(mContext,false) {
                    @Override
                    protected void _onNext(MapResult data) {
                        if(data.getDetails().size() > 0){
                            mView.addMarks(data);
                        }else{
                            ToastUtils.showMessage(mContext, "暂无数据");
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void getSearchListInfo(String parentId){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getFloorInfo(parentId),
                new ProgressSubscriber<StepFloorBean>(mContext,false) {
                    @Override
                    protected void _onNext(StepFloorBean data) {
                        mView.showSearchResult(data);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }
}
